<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class PostsAction extends AdminAction
{

    protected $Category;
	protected $Question;
	protected $Answer;
	protected $Comment;
	protected $Tag;


	//初始化
	public function _initialize()
	{
		parent::_initialize();
		$this->Category = D('Category');
		$this->Question = D('Question');
		$this->Answer   = D('Answer');
		$this->Comment  = D('Comment');
		$this->Tag      = D('Tag');
	}


	//搜索问题
	public function searchQuestion()
	{
		$Category = D('Category');
		$cateOptions = $Category->getCategoryOptions(0, 3);
		
		$this->assign('categoryOptions', $cateOptions);
		$this->assign('op', 'searchForm');
		$this->assign('answerCountOptions', L('answer_count_options'));
		$this->assign('questionStatusOptions', L('question_status_options'));
		$this->display();
	}


	//问题列表
	public function questionList()
	{
		import('ORG.Util.Page');
		$where = $order = array();
		$where['is_visible'] = 1;
		$parameter = '';

		if (isset($_GET['category_id']) && 'all' != $_GET['category_id'])
		{
			$where['category_id'] = array('in', get_sub_category_ids($_GET['category_id'], 1));
			$parameter .= 'category_id='.$_GET['category_id'].'&';
		}
		if(!empty($_GET['poster_name']))
		{
			$where['poster_name'] = $_GET['poster_name'];
			$parameter .= 'poster_name='.$_GET['poster_name'].'&';
		}
		if (!empty($_GET['title']))
		{
			$where['title'] = array('like', '%'.$_GET['title'].'%');
			$parameter .= 'title='.$_GET['title'].'&';
		}
		if (isset($_GET['status']) && 'all' != $_GET['status'])
		{
			$where['status'] = $_GET['status'];
			$parameter .= 'status='.$_GET['status'].'&';
		}
		if (!empty($_GET['create_time']))
		{
			$where['create_time'] = array($_GET['create_time_select'], strtotime($_GET['create_time']));
			$parameter .= 'create_time_select='.$_GET['create_time_select'].'&';
			$parameter .= 'create_time='.$_GET['create_time'].'&';
		}
		if (isset($_GET['answer_count']) && 'all' != $_GET['answer_count'])
		{
			$where['answer_count'] = (0 != $_GET['answer_count']) ? array('between', $_GET['answer_count']) : 0;
			$parameter .= 'answer_count='.$_GET['answer_count'].'&';
		}

		$total = $this->Question->where($where)->count();
		$Page = new Page($total);
		$Page->parameter = $parameter;
		$pagination = $Page->show();

		$order['question_id'] = 'desc';
		$questionList = $this->Question->where($where)->limit($Page->firstRow.','.$Page->listRows)->order($order)->select();

		$this->assign('total', $total);
		$this->assign('pagination', $pagination);
		$this->assign('questionList', $questionList);
		$this->display();
	}


	//管理问题
	public function manageQuestion()
	{
        if ($this->isPost())
        {
            $this->assign('jumpUrl', __URL__.'/questionList');
            $where = array('question_id' => array('in', $_POST['mods']));
            if (empty($_POST['mods']))
            {
            	$this->error(L('no_post_selected'));
            }
            switch ($_POST['action'])
            {
            	case 'delete':
                    if (1 == $_POST['delete_confirm'])
                    {
                        $this->Question->deleteQuestionByID($_POST['mods']);
                        $this->success(L('question_deleted_succeed'));
                    }
                    else
                    {
                    	$this->redirect('Admin-Posts/questionList');
                    }
            		break;
            	case 'move':
            	    $this->Question->where($where)->setField('category_id', $_POST['category_id']);
            	    $this->success(L('question_moved_succeed'));
            	    break;
            	case 'lock':
            	    if (1 == $_POST['is_locked'])
            	    {
            	    	$this->Question->lockQuestionByID($_POST['mods']);
            	    }
            	    else
            	    {
            	        $this->Question->unlockQuestionByID($_POST['mods']);
            	    }
            	    $this->success(L('question_locked_succeed'));
            	    break;
            }
        }
        else
        {
            if (empty($_POST['mods']))
            {
                $this->redirect('Admin-Posts/questionList');
            }

            $boolRadio = array(1 => L('yes'), 0 => L('no'));

            $this->assign('deleteConfirmRadios', $boolRadio);
            $this->assign('deleteConfirmChecked', 1);

            $lockRadios = array(1 => L('lock_question'), 0 => L('unlock_question'));
            $this->assign('lockRadios', $lockRadios);
            $this->assign('lockChecked', -1);

            $categoryOptions = $this->Category->getCategoryOptions($categoryID, 3);
			$this->assign('categoryOptions', $categoryOptions);

			$where = array('question_id' => array('in', $_POST['mods']));
			$questionList = $this->Question->where($where)->select();
			$this->assign('questionList', $questionList);

            $action = array_keys($_POST['action']);
            $this->assign('action', $action[0]);
            $this->assign('mods', $_POST['mods']);
            $this->display();
        }
	}


	//管理回收站
	public function manageRecycleBin()
	{
	    $this->redirect('Admin-Posts/approveQuestion', array('mode'=>'suspend'));
	}


	//清空回收站
	public function emptyRecycleBin()
	{
		if (isset($_POST['action']['confirm']))
		{
		    $where = array('is_visible' => -1);

            $this->Comment->where($where)->delete();
            $this->Answer->where($where)->delete();
            $this->Question->where($where)->delete();

            $this->redirect('Admin-Posts/manageRecycleBin');
		}
		elseif (isset($_POST['action']['cancel']))
		{
			$this->redirect('Admin-Posts/manageRecycleBin');
		}
		else
		{
		    $this->confirm(L('empty_recycle_bin_confirm'));
		}
	}

}

/* End of file QuestionAction.class.php */
/* Location: ./App/Lib/Action/Admin/QuestionAction.class.php */